<?php
/**
 * DouPHP
 * --------------------------------------------------------------------------------------------------
 * 版权所有 2013-2015 漳州豆壳网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.douco.com
 * --------------------------------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在遵守授权协议前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 * 授权协议：http://www.douco.com/license.html
 * --------------------------------------------------------------------------------------------------
 * Author: DouCo
 * Release Date: 2015-10-16
 */
define('IN_DOUCO', true);

require (dirname(__FILE__) . '/include/init.php');

// rec操作项的初始化
$rec = $check->is_rec($_REQUEST['rec']) ? $_REQUEST['rec'] : 'default';
$smarty->assign('rec', $rec);
$smarty->assign('cur', 'order');
/**
 * +----------------------------------------------------------
 * 订单列表
 * +----------------------------------------------------------
 */
if ($rec == 'default') {
    $smarty->assign('ur_here', $_LANG['order']);
    $smarty->assign('action_link', array (
            'text' => $_LANG['return_list'],
            'href' => 'javascript:history.back();' 
    ));

    //fliter-----//
    $state    = $_REQUEST['state'];
    $paystate = $_REQUEST['paystate'];
    $user_id  = $_REQUEST['user_id'];
    $order_no = $_REQUEST['order_no'];

    $where = ' WHERE 1 ';
    if($state != '')
        $where .=  " AND state='$state'";
        $get .= '&state=' . $state;
    if($paystate != '')
        $where .=  " AND paystate='$paystate' ";
        $get .= '&paystate=' . $paystate;
    if($user_id != '')
        $where .=  " AND user_id='$user_id' ";
        $get .= '&user_id=' . $user_id;
    if($order_no != '')
        $where .=  " AND order_no='$order_no' ";
        $get .= '&order_no=' . $order_no;

    // 分页
    $page = $check->is_number($_REQUEST['page']) ? $_REQUEST['page'] : 1;
    $page_url = 'order.php?1=1';
    $limit = $dou->pager('order', 15, $page, $page_url, $where, $get);

    $sql = "SELECT * FROM " . $dou->table('order') . " $where ORDER BY id DESC ".$limit;
    $query = $dou->query($sql);
    while ($row = $dou->fetch_array($query)) {
        $add_time = date("Y-m-d H:i:s", $row['add_time']);
        
        $order_list[$row[id]] = $row;
        $order_list[$row[id]]['add_time']   = $add_time;
    }
    // 赋值给模板
    $smarty->assign('user_id', $user_id);
    $smarty->assign('order_list', $order_list);
    $smarty->display('order.htm');
} 

elseif ($rec == 'detail') {
    $smarty->assign('ur_here', $_LANG['order_detail']);
    $smarty->assign('action_link', array ('text' => $_LANG['return_list'],'href' => 'javascript:history.back();' ));

    //订单信息
    $order_no = $_REQUEST['order_no'] ? $_REQUEST['order_no'] : '';
    $sql = "SELECT * FROM " . $dou->table('order') . " WHERE order_no='$order_no' LIMIT 1 ";
    $order['info']   = $GLOBALS['dou']->get_row($sql);
    $order['region'] = get_region_info($order['info'][province], $order['info'][city], $order['info'][area]); 

    //预定信息
    $sql = "SELECT * FROM " . $dou->table('order_booking') . " WHERE order_no='$order_no' ";
    $order['booking'] = $GLOBALS['dou']->get_all($sql);

    $smarty->assign('today', date('Ymd'));
    $smarty->assign('order', $order);
    $smarty->display('order_detail.htm');
}

elseif ($rec == 'del') {
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : $dou->dou_msg($_LANG['illegal'], 'order.php');
    $order_no = $dou->get_one("SELECT order_no FROM " . $dou->table('order') . " WHERE id = '$id'");
    
    if (isset($_POST['confirm']) ? $_POST['confirm'] : '') {
        
        $dou->delete($dou->table('order'), "id = $id", 'order.php');
        $dou->delete($dou->table('order_booking'), "order_no='$order_no'", 'order.php');
        $dou->create_admin_log($_LANG['order_del'] . ': ' . $order_no);
    } else {
        $_LANG['del_check'] = preg_replace('/d%/Ums', $order_no, $_LANG['del_check']);
        $dou->dou_msg($_LANG['del_check'], 'order.php', '', '30', "order.php?rec=del&id=$id");
    }
} 

elseif($rec == 'action'){
    $action = i('action','');
    $id     = i('id','0');
    if(in_array($action, array('noon_state','night_state'))){
        $dou->query("UPDATE " . $dou->table('order_booking') . " SET {$action} = '1' WHERE id='$id' AND order_no='$_REQUEST[order_no]'");
        $dou->dou_header($_SERVER['HTTP_REFERER']);
    }
    
}


?>